package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.DateUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.project.dao.ProjectManageDao;
import io.renren.modules.project.service.ProjectManageService;
import io.renren.modules.project.vo.ProjectVoEntity;
import io.renren.modules.project.vo.RecycleVoEntity;
import io.renren.modules.set.entity.WorkGroupEntity;
import io.renren.modules.set.service.WorkGroupService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


@Service("ProjectManageService")
public class ProjectManageServiceImpl extends ServiceImpl<ProjectManageDao, ProjectVoEntity> implements ProjectManageService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private WorkGroupService workGroupService;

    /**
     * 获取项目管理分页列表
     * @param params
     * @return
     */
    @Override
    public PageUtils getProjectManagPage(Map<String, Object> params){
        // 获取当前用户角色
        SysUserEntity curUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        List<Long> roleList = sysUserRoleService.queryRoleIdList(curUser.getUserId());

        // 角色是管理员
        if( roleList.indexOf(1L) >= 0){
            ;
        }   // 角色是质检、核算、生产负责人
        else if ( roleList.indexOf(10L) >= 0 || roleList.indexOf(3L) >= 0 || roleList.indexOf(6L) >= 0  ) {
            params.put("projectchargeAccount", curUser.getUseraccount());
        }
        //如果当前角色有项目负责人的角色，而且不是管理员角色
        else if(roleList.indexOf(4L) >= 0 ){
            params.put("projectchargeAccount", curUser.getUseraccount());
            WorkGroupEntity groupEntity = workGroupService.selectOne(
                    new EntityWrapper<WorkGroupEntity>().eq( "head_id",curUser.getUserId())
            );
//            // 并且角色不是质检、核算、生产负责人
//            if( roleList.indexOf(3L) == -1 && roleList.indexOf(3L) == -1 && roleList.indexOf(6L) == -1 )
                // 如果项目负责人 不属于任何组
                if ( groupEntity != null) {
                    params.put("groupId" , groupEntity.getId());
                }
        }

        Page<ProjectVoEntity> pagnation = new Query<ProjectVoEntity>(params).getPage();
        pagnation = pagnation.setRecords( baseMapper.getProjectManagPage(pagnation , params ) );
        return new PageUtils(pagnation);
    }

    @Override
    public List<ProjectVoEntity> get90PList(Map<String, Object> params) {
        return this.baseMapper.get90PList(params);
    }

    @Override
    public List<ProjectVoEntity> getIsOutputList(){
        return this.baseMapper.getIsOutputList();
    }

    /**
     * 项目管理列表
     * @param params
     * @return
     */
    public List<ProjectVoEntity> getProjectManagList(Map<String, Object> params){
        return baseMapper.getProjectManagPage(params);
    }


    /**
     * 导出项目excel
     * @return
     */
    @Override
    public Workbook exportProExcel(Map<String, Object> params) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat mdateFormat = new SimpleDateFormat("yyyy-MM");
        String radio = (String) params.get("radio");

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        int rowIndex = 0;

        //生成一个表格，设置表格名称
        HSSFSheet sheet = workbook.createSheet("sheet1");
        // 字段字体设置
        HSSFFont tfont = workbook.createFont();
        tfont.setFontName("黑体");
        tfont.setFontHeightInPoints((short) 12);
        tfont.setBold(true);   //字体加粗
        // 字段表格样式
        HSSFCellStyle tstyle = workbook.createCellStyle();
        tstyle.setFont(tfont);
        tstyle.setVerticalAlignment(VerticalAlignment.CENTER);  // 垂直居中

        // 字段行
        HSSFRow tRow = sheet.createRow(rowIndex++);
        tRow.setHeight((short) 420);
        HSSFCell tcell;
        switch (radio) {
            // 导出项目列表
            case "1":
                List<ProjectVoEntity> list = this.getProjectManagList(params);
                //设置表格列宽度
                sheet.setColumnWidth(0, 40 * 100);
                sheet.setColumnWidth(1, 130 * 100);
                sheet.setColumnWidth(2, 90 * 100);
                sheet.setColumnWidth(3, 40 * 100);
                sheet.setColumnWidth(4, 40 * 100);
                sheet.setColumnWidth(5, 40 * 100);
                sheet.setColumnWidth(6, 40 * 100);
                sheet.setColumnWidth(7, 40 * 100);
                sheet.setColumnWidth(8, 40 * 100);
                sheet.setColumnWidth(9, 40 * 100);
                sheet.setColumnWidth(10, 40 * 100);
                sheet.setColumnWidth(11, 40 * 100);
                sheet.setColumnWidth(12, 40 * 100);
                sheet.setColumnWidth(13, 40 * 100);
                sheet.setColumnWidth(14, 40 * 100);
                sheet.setColumnWidth(15, 40 * 100);
                sheet.setColumnWidth(16, 40 * 100);
                sheet.setColumnWidth(17, 40 * 100);
                sheet.setColumnWidth(18, 40 * 100);

                tcell = tRow.createCell(0);
                tcell.setCellValue(new HSSFRichTextString("项目编号"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(1);
                tcell.setCellValue(new HSSFRichTextString("项目名称"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(2);
                tcell.setCellValue(new HSSFRichTextString("项目类型"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(3);
                tcell.setCellValue(new HSSFRichTextString("委托单位"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(4);
                tcell.setCellValue(new HSSFRichTextString("生产负责人"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(5);
                tcell.setCellValue(new HSSFRichTextString("项目负责人"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(6);
                tcell.setCellValue(new HSSFRichTextString("项目启动时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(7);
                tcell.setCellValue(new HSSFRichTextString("项目开工时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(8);
                tcell.setCellValue(new HSSFRichTextString("作业完成时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(9);
                tcell.setCellValue(new HSSFRichTextString("质检完成时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(10);
                tcell.setCellValue(new HSSFRichTextString("结算时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(11);
                tcell.setCellValue(new HSSFRichTextString("作业工期"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(12);
                tcell.setCellValue(new HSSFRichTextString("质检工期"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(13);
                tcell.setCellValue(new HSSFRichTextString("返修天数"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(14);
                tcell.setCellValue(new HSSFRichTextString("暂停天数"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(15);
                tcell.setCellValue(new HSSFRichTextString("作业超时天数"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(16);
                tcell.setCellValue(new HSSFRichTextString("质检超时天数"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(17);
                tcell.setCellValue(new HSSFRichTextString("是否提交成果"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(18);
                tcell.setCellValue(new HSSFRichTextString("是否签收成果"));
                tcell.setCellStyle(tstyle);

                // 各组各项目评分明细
                for (ProjectVoEntity entity : list) {
                    HSSFRow cRow = sheet.createRow(rowIndex++);
                    cRow.setHeight((short) 280);
                    HSSFCell cell;
                    cell = cRow.createCell(0);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectNo()));
                    cell = cRow.createCell(1);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectName()));
                    cell = cRow.createCell(2);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectType()));
                    cell = cRow.createCell(3);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectAuthorize()));
                    cell = cRow.createCell(4);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectProduce()));
                    cell = cRow.createCell(5);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectCharge()));
                    cell = cRow.createCell(6);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectStartDateTime() == null ? "" :dateFormat.format(entity.getProjectStartDateTime())));
                    cell = cRow.createCell(7);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectBegunDateTime() == null ? "" :dateFormat.format(entity.getProjectBegunDateTime())));
                    cell = cRow.createCell(8);
                    cell.setCellValue(new HSSFRichTextString(entity.getWFinishDateTime() == null ? "" :dateFormat.format(entity.getWFinishDateTime())));
                    cell = cRow.createCell(9);
                    cell.setCellValue(new HSSFRichTextString(entity.getQFinishDateTime() == null ? "" :dateFormat.format(entity.getQFinishDateTime())));
                    cell = cRow.createCell(10);
                    cell.setCellValue(new HSSFRichTextString(entity.getCutOffTime() == null ? "" :mdateFormat.format(entity.getCutOffTime())));
                    cell = cRow.createCell(11);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectWorkDate() == null ? "" : entity.getProjectWorkDate().toString()));
                    cell = cRow.createCell(12);
                    cell.setCellValue(new HSSFRichTextString( entity.getProjectQualityDate() == null ? "" : entity.getProjectQualityDate().toString()));
                    cell = cRow.createCell(13);
                    cell.setCellValue(new HSSFRichTextString( entity.getBackDateNum() == null ? "0" : entity.getBackDateNum().toString()));
                    cell = cRow.createCell(14);
                    cell.setCellValue(new HSSFRichTextString( entity.getSuspendDay() == null ? "0" : entity.getSuspendDay().toString()));
                    // 作业超时天数
                    Float suspend = entity.getSuspendDay() == null ? 0f :entity.getSuspendDay();
                    Float backNum = entity.getBackDateNum() == null ? 0f :entity.getBackDateNum();
                    Float workDay = entity.getProjectWorkDate() == null ? 0f : (float)(Math.ceil(entity.getProjectWorkDate() * 3) * 0.5f);
                    String workoutDay = "-";
                    if( !(entity.getProjectBegunDateTime() == null || entity.getWFinishDateTime() == null) ){
                        Float day = (Long)((entity.getWFinishDateTime().getTime() - entity.getProjectBegunDateTime().getTime()) / (1000*3600*12)) * 0.5f
                                - ( suspend + backNum + workDay);
                        workoutDay = String.valueOf( day < 0 ? 0: day);
                    }
                    cell = cRow.createCell(15);
                    cell.setCellValue(new HSSFRichTextString( workoutDay));
                    // 质检超时时间
                    String checkoutDay = "-";
                    if( !(entity.getQFinishDateTime() == null || entity.getWFinishDateTime() == null) ){
                        Float day =  (Long)((entity.getQFinishDateTime().getTime() - entity.getWFinishDateTime().getTime()) / (1000*3600*12)) * 0.5f
                                - (entity.getProjectQualityDate() == null ? 0f :  (float)(Math.ceil(entity.getProjectQualityDate() * 3) * 0.5f) );
                        checkoutDay = String.valueOf( day < 0f ? 0f: day);
                    }
                    cell = cRow.createCell(16);
                    cell.setCellValue(new HSSFRichTextString( checkoutDay));
                    cell = cRow.createCell(17);
                    cell.setCellValue(new HSSFRichTextString(entity.getIsSach() != null && entity.getIsSach() == 1? "是": "否"));
                    cell = cRow.createCell(18);
                    cell.setCellValue(new HSSFRichTextString(entity.getIsSign() != null && entity.getIsSign() == 1? "是": "否"));
                }
                break;
            // 导出进度统计列表
            case "2":
                List<ProjectVoEntity> qlist = this.get90PList(params);
                //设置表格列宽度
                sheet.setColumnWidth(0, 40 * 100);
                sheet.setColumnWidth(1, 130 * 100);
                sheet.setColumnWidth(2, 90 * 100);
                sheet.setColumnWidth(3, 80 * 100);
                sheet.setColumnWidth(4, 40 * 100);
                sheet.setColumnWidth(5, 40 * 100);

                tcell = tRow.createCell(0);
                tcell.setCellValue(new HSSFRichTextString("项目编号"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(1);
                tcell.setCellValue(new HSSFRichTextString("项目名称"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(2);
                tcell.setCellValue(new HSSFRichTextString("项目负责人"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(3);
                tcell.setCellValue(new HSSFRichTextString("进度90%的提交时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(4);
                tcell.setCellValue(new HSSFRichTextString("作业完成时间"));
                tcell.setCellStyle(tstyle);
                tcell = tRow.createCell(5);
                tcell.setCellValue(new HSSFRichTextString("是否异常"));
                tcell.setCellStyle(tstyle);

                // 字段字体设置
                HSSFFont rfont = workbook.createFont();
                rfont.setFontName("黑体");
                rfont.setFontHeightInPoints((short) 12);
                rfont.setColor(HSSFFont.COLOR_RED);
                rfont.setBold(true);   //字体加粗
                // 字段表格样式
                HSSFCellStyle rstyle = workbook.createCellStyle();
                rstyle.setFont(rfont);

                for (ProjectVoEntity entity : qlist) {
                    HSSFRow cRow = sheet.createRow(rowIndex++);
                    cRow.setHeight((short) 280);
                    HSSFCell cell;
                    cell = cRow.createCell(0);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectNo()));
                    cell = cRow.createCell(1);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectName()));
                    cell = cRow.createCell(2);
                    cell.setCellValue(new HSSFRichTextString(entity.getProjectCharge()));
                    cell = cRow.createCell(3);
                    if (entity.getScheduleRate() != null && entity.getScheduleRate() == 90) {
                        cell.setCellValue(new HSSFRichTextString(entity.getScheduleTime() == null ? "" :dateFormat.format(entity.getScheduleTime())));
                    }
                    cell = cRow.createCell(4);
                    cell.setCellValue(new HSSFRichTextString(entity.getWFinishDateTime() == null ? "" :dateFormat.format(entity.getWFinishDateTime())));
                    cell = cRow.createCell(5);
                    if ( entity.getScheduleTime() != null && entity.getWFinishDateTime() != null && entity.getScheduleRate() == 90 &&
                            DateUtil.getDaySub(DateUtil.getFormatDay(entity.getScheduleTime()),DateUtil.getFormatDay(entity.getWFinishDateTime())) > 1 ) {
                        cell.setCellValue(new HSSFRichTextString("异常"));
                        cell.setCellStyle(rstyle);
                    } else {
                        cell.setCellValue(new HSSFRichTextString(""));
                    }

                }
                break;
            default:

                break;
        }


        return workbook;
    }
}
